package com.linan.util;

import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.IOException;

public class FastFdsUtil {

   private static StorageClient storageClient;

   static {
       // 加载配置文件 配置文件中的内容就是tracker服务的地址
       try {
           ClientGlobal.init("fdfs_client.conf");
       } catch (IOException e) {
           throw new RuntimeException(e);
       } catch (MyException e) {
           throw new RuntimeException(e);
       }

       //创建一个trackerClient对象
       TrackerClient trackerClient = new TrackerClient();

       //创建链接 获得一个TrackerServer对象
       TrackerServer trackerServer = null;
       try {
           trackerServer = trackerClient.getConnection();
       } catch (IOException e) {
           throw new RuntimeException(e);
       }

       // 创建一个StorageServer的引用
       StorageServer storageServer = null;

       //创建一个StorageClient对象
       storageClient = new StorageClient(trackerServer, storageServer);
   }

   public static   String[] uploadLocalFile(String filename, String file_ext_name) throws MyException, IOException {

       //使用StorageClient对象上传图片
       String[] strings = storageClient.upload_file(filename, file_ext_name,null);
       return strings;

   }

   public static String[]  uploadFile(byte [] bytes,String ext_name) throws MyException, IOException {
       return storageClient.upload_file(bytes, ext_name, null);
   }

}